import prisma from './prisma.service';

class FriendService {
    addFriend(userId: string, friendId: string) {
        return prisma.$transaction([
            prisma.userFriend.create({
                data: {
                    userId,
                    friendId
                }
            }),
            prisma.userFriend.create({
                data: {
                    userId: friendId,
                    friendId: userId
                }
            })
        ]);
    }

    createFriend(userId: string, friendId: string) {
        return prisma.userFriend.create({
            data: {
                userId,
                friendId
            },
            include: {
                friend: true
            }
        })
    }

    removeFriend(userId: string, friendId: string) {
        return prisma.userFriend.delete({
            where: {
                userId_friendId: {
                    userId,
                    friendId
                }
            },
            include: {
                friend: true
            }
        })
    }

    getUserFriend(userId: string, friendId: string) {
        return prisma.userFriend.findUnique({
            where: {
                userId_friendId: { userId, friendId }
            },
            include: {
                friend: true
            }
        });
    }

    getFriends(userId: string) {
        return prisma.userFriend.findMany({
            where: { userId },
            include: {
                friend: true
            }
        });
    }
}

const friendService = new FriendService();
export default friendService;
